﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Globalization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Ko2.Portal;

using System.Diagnostics;

namespace JobShopsDiscussion_v01_App18
{

    [Guid("BAEAC876-E8B2-4800-926D-227CC5C234CC")]
    public class JobShopsDiscussion_JobClass : SPJobDefinition
    {

        #region Propertys

        private string _DisplayName = @"(Ko) Job JobShopsDiscussion_v01_App18";
        public override string DisplayName { get { return _DisplayName; } }

        private string _Description = @"App18, ver. 1.0.0.5";
        public override string Description { get { return _Description; } }

        #endregion Propertys

        #region Members

        string siteUrl = @"http://localhost/shops/";

        #endregion

        #region Constructors
        public JobShopsDiscussion_JobClass()
            : base()
        {
            this.Title = DisplayName;
        }
        public JobShopsDiscussion_JobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
            this.Title = DisplayName;
        }
        public JobShopsDiscussion_JobClass(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = DisplayName;
        }
        #endregion Constructors

        #region Job Methods
        public Exception Execute(Guid contentDbId, string siteStr)
        {
            Exception ret = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                Ko2.Portal.Env env = new Ko2.Portal.Env();
                Imap.KoExchWebService o = new Imap.KoExchWebService();
                string uid = env.GetAppSetting("FarmUserUID");
                if (String.IsNullOrEmpty(uid))
                {
                    ret = new Exception("Error: env.GetAppSetting('FarmUserUID')");
                }
                if (ret == null)
                {
                    string[] separator = { @"\" };
                    string[] uid2 = uid.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    if (uid2.Length < 2)
                    {
                        ret = new Exception("Error: env.GetAppSetting('FarmUserUID')");
                    }
                    if (ret == null)
                    {
                        uid = uid2[1];
                        string pwd = env.GetAppSetting("FarmUserPwd");
                        if (String.IsNullOrEmpty(pwd))
                        {
                            ret = new Exception("Error: env.GetAppSetting('FarmUserPwd')");
                        }
                        if (ret == null)
                        {
                            string DomainName = env.GetAppSetting("ProxyDomainName");
                            if (String.IsNullOrEmpty(DomainName))
                            {
                                ret = new Exception("Error: env.GetAppSetting('ProxyDomainName')");
                            }
                            if(ret == null)
                                ret = o.Get_SP_Q_Msg(uid, pwd, DomainName, "SP10_spfarm@korablik.ru");
                        }
                    }
                }

            });

            if (ret != null)
            {
#if DEBUG
                Debug.WriteLine("Error: " + ret.Message);
#endif                
            }

            return ret;
        }
        public override void Execute(Guid contentDbId)
        {
            Exception err = Execute(contentDbId, siteUrl);
        }
        #endregion

    }

}
